package com.avic.rong.goodsSort.adapter.secondListAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllWdBean;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 */
public class GoodsSortWdShowRvAdapter extends RecyclerView.Adapter<GoodsSortWdShowRvAdapter.GoodsSortWdShowRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllWdBean.DataBean> beanList = new ArrayList<>();
    public GoodsWdListener goodsWdListener;

    public GoodsWdListener getGoodsWdListener() {
        return goodsWdListener;
    }

    public void setGoodsWdListener(GoodsWdListener goodsWdListener) {
        this.goodsWdListener = goodsWdListener;
    }

    public GoodsSortWdShowRvAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void setBeanList(List<GoodsSortSearchAllWdBean.DataBean> beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public GoodsSortWdShowRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_goods_sort_ts_rv, viewGroup, false);
        return new GoodsSortWdShowRvViewHolder(view);
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull GoodsSortWdShowRvViewHolder holder, int i) {

        //设置标题筛选高亮
//        if (beanList != null){
//            SpannableString spannableString = new SpannableString(beanList.get(i).getTitle());
//            int startIndex = beanList.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
//            if (startIndex>=0){
//                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
//                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
//                holder.title.setText(spannableString);
//            }else {
        holder.title.setText(beanList.get(i).getTitle());
//            }
//        }
        DecimalFormat df = new DecimalFormat("###0.00");
        String format = df.format(beanList.get(i).getPrice());

        if (StringUtils.isEmpty(beanList.get(i).getImg()+"")) {
            holder.img.setVisibility(View.GONE);
            holder.layout.setVisibility(View.VISIBLE);
            holder.up.setVisibility(View.VISIBLE);
            holder.down.setVisibility(View.VISIBLE);
            holder.view_xian.setVisibility(View.VISIBLE);
            holder.up.setText(beanList.get(i).getNo());
            holder.down.setText(beanList.get(i).getSource());
        }else{
            holder.layout.setVisibility(View.GONE);
            holder.up.setVisibility(View.GONE);
            holder.down.setVisibility(View.GONE);
            holder.view_xian.setVisibility(View.GONE);
            holder.img.setVisibility(View.VISIBLE);
            Glide.with(context).load(beanList.get(i).getImg()).placeholder(R.mipmap.doc_default).into(holder.img);
        }

        holder.desc.setText(beanList.get(i).getNo());
        holder.price.setText("¥" + format);
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "splite:" + split[2]);
                String skuStr = "{" + "\"" + split[2].toString() + "\"" + ":" + "1}";
                goodsWdListener.addWdCar(skuStr);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = beanList.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);

                Intent intent = DetailProductActivity.getIntent(context, split[2]);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    class GoodsSortWdShowRvViewHolder extends RecyclerView.ViewHolder {
        TextView title, desc, price,down,up;
        ImageView car, img;
        LinearLayout layout;
        View view_xian;
        public GoodsSortWdShowRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.goods_sort_ts_rv_img);
            price = itemView.findViewById(R.id.goods_sort_ts_rv_price);
            down = itemView.findViewById(R.id.goods_wd_text_down);
            layout = itemView.findViewById(R.id.goods_wd_text_layout);
            up = itemView.findViewById(R.id.goods_wd_text_up);
            view_xian = itemView.findViewById(R.id.goods_wd_text_view);
            title = itemView.findViewById(R.id.goods_sort_ts_rv_title);
            car = itemView.findViewById(R.id.goods_sort_ts_rv_car);
            desc = itemView.findViewById(R.id.goods_sort_ts_rv_desc);
        }
    }

    public interface GoodsWdListener {
        void addWdCar(String skuStr);
    }
}
